<?php
/**
 * Created by PhpStorm
 * Date: 2023/7/21
 * Time:10:23
 */

namespace app\api\controller;

use app\admin\model\Ordderdistribution;
use app\admin\model\Order;
use app\common\controller\Api;
use app\common\utils\FeiUtil;
use app\common\utils\WechatPayUtil;
use think\Log;

/**
 * @APiInternal()
 */
class Pay extends Api
{
    protected $noNeedLogin = "*";
    protected $noNeedRight = "*";

    /**
     * 微信支付回调
     */
    public function notify()
    {
        $getCallBackData = file_get_contents('php://input'); //接收来自微信的回调数据

        $getData = new WechatPayUtil();

        $disposeReturnData = json_decode($getCallBackData, true);  //将变量由json类型数据转换为数组

        $associatedData = $disposeReturnData['resource']['associated_data'];  //获取associated_data数据,附加数据
        $nonceStr = $disposeReturnData['resource']['nonce'];                  //获取nonce数据,加密使用的随机串
        $ciphertext = $disposeReturnData['resource']['ciphertext'];           //获取ciphertext数据,base64编码后的数据密文

        $result = $getData->decryptToString($associatedData, $nonceStr, $ciphertext); //调用微信官方给出的方法将解密后的数据赋值给变量

        $array_data = json_decode($result, true);  //将解密后的数据转换为数组
        file_put_contents('./decryptReturnData.json', $result); //将数据以JSON格式储存到decryptReturnData.json文件中

        if ($array_data['trade_state'] == 'SUCCESS') {
            $this->clData($array_data);
            return "SUCCESS";
        }
        return "SUCCESS";

    }

    /**
     * 订单异步处理
     * {"mchid":"1661895787","appid":"wx8e2ca39b04eac894","out_trade_no":"2023121916492149514851","transaction_id":"4200002120202312192485717559","trade_type":"JSAPI","trade_state":"SUCCESS","trade_state_desc":"支付成功","bank_type":"BOC_DEBIT","attach":"2","success_time":"2023-12-19T16:49:27+08:00","payer":{"openid":"oXRN462EkVZvybBJ27F2PNf4Q8ts"},"amount":{"total":399,"payer_total":399,"currency":"CNY","payer_currency":"CNY"}}
     */
    protected function clData($array_data)
    {
        //商品订单
        $model2 = new FeiUtil();
        if ($array_data['attach'] == 1) {
            $model = new Order();
            $order = $model->where("order_no", $array_data['out_trade_no'])->find();
            if ($order['pay_status'] == 1) {
                $order->pay_status = 2;
                $order->pay_time = time();
                $order->trade_on = $array_data['transaction_id'];
                $order->save();
            }
        } elseif ($array_data['attach'] == 2) {//地址订单

            $model = new Ordderdistribution();
            $order = $model->where("order_no", $array_data['out_trade_no'])->find();
            if ($order['zl_money'] > 0) {
                $user = \app\common\model\User::where("id", $order['user_id'])->find();
                $user->zl_money = $user['zl_money'] - $order['zl_money'];
                $user->save();
            }

            $order->pay_status = 2;
            $order->pay_time = time();
            $order->trade_on = $array_data['transaction_id'];
            $order->save();
        }

    }

    public function refund_notify()
    {
        $data = file_get_contents('php://input'); //接收来自微信的回调数据
        file_put_contents('./data.json', $data); //将数据以JSON格式储存到decryptReturnData.json文件中
        $getData = new WechatPayUtil();

        $disposeReturnData = json_decode($data, true);  //将变量由json类型数据转换为数组

        $associatedData = $disposeReturnData['resource']['associated_data'];  //获取associated_data数据,附加数据
        $nonceStr = $disposeReturnData['resource']['nonce'];                  //获取nonce数据,加密使用的随机串
        $ciphertext = $disposeReturnData['resource']['ciphertext'];           //获取ciphertext数据,base64编码后的数据密文

        $result = $getData->decryptToString($associatedData, $nonceStr, $ciphertext); //调用微信官方给出的方法将解密后的数据赋值给变量
        $model = new Order();
        $array_data = json_decode($result, true);  //将解密后的数据转换为数组
        file_put_contents('./decryptReturnData2.json', $result); //将数据以JSON格式储存到decryptReturnData.json文件中
        Log::error("退款回调：" . $array_data['out_refund_no'] . "==" . $result);
        $order = $model->where("tk_no", $array_data['out_refund_no'])->find();
        if ($array_data['refund_status'] == 'SUCCESS') {
            $order->is_tk = 2;

        } else {
            $order->is_tk = 1;
        }
        $order->save();
        return "SUCCESS";

    }

}